package com.xiong.wuliao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xiong.wuliao.R;
import com.xiong.wuliao.entity.HomeStorys;
import com.xiong.wuliao.interfaces.OnLoveClickListener;

import java.util.List;

/**
 * 首页除"精选"页外的其它页的Adapter
 */
public class MyHomeListAdapter extends BaseAdapter implements View.OnClickListener {
    public Context context;
    public LayoutInflater inflater;
    public List<HomeStorys> lists;
	public OnLoveClickListener listener=null;

	public void setLoveListener(OnLoveClickListener listener){
		this.listener=listener;
	}

    public MyHomeListAdapter(Context context,List<HomeStorys> lists) {
        this.context = context;
        this.lists = lists;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HomeStorys storys = lists.get(position);
        ViewHolder holder = null;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.home_adapter_item,null);
            holder = new ViewHolder();
            holder.itemImageView = (ImageView) convertView.findViewById(R.id.itemImageView);
            holder.itemTextView = (TextView) convertView.findViewById(R.id.itemTextView);
            holder.itemImageAgree = (ImageView) convertView.findViewById(R.id.itemImageAgree);
            holder.itemTextViewArgee = (TextView) convertView.findViewById(R.id.itemTextViewArgee);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        Picasso.with(context).load(storys.getCover_image_url()).into(holder.itemImageView);
        holder.itemTextView.setText(storys.getTitle());
       boolean isLiked = lists.get(position).isLiked();
		if (isLiked){
			holder.itemImageAgree.setImageResource(R.mipmap.ic_feed_favourite_selected);
		}else {
			holder.itemImageAgree.setImageResource(R.mipmap.ic_feed_favourite_normal);
		}
        holder.itemTextViewArgee.setText(storys.getLikes_count());

		holder.itemImageAgree.setTag(position);
		holder.itemImageAgree.setOnClickListener(this);

        return convertView;
    }

	@Override
	public void onClick(View v) {
		if (listener!=null){
			listener.loveClick(v);
		}
	}

	static class ViewHolder{
        public ImageView itemImageView; // 显示List背景的图片
        public TextView itemTextView; // 显示标题的文本
        public ImageView itemImageAgree; // 显示是否点赞的图标
        public TextView itemTextViewArgee; // 显示点赞次数的文字
    }
}
